
const router = require('koa-router')();
const util = require("../common/utils")
const { sequelize } = require('../common/dbs')

const userModel = require("../models/my")
const userinfo = userModel(sequelize);

// 分页查询
router.get('/detail', async (ctx) => {
    try {
        let reqQuery = ctx.request.query;
        let userData = await userinfo.findAndCountAll(
            {
                attributes: {   // exclude排除
                    exclude: ['id','deletedAt'],
                },
                limit: reqQuery.pageSize?parseInt(reqQuery.pageSize):10,
                offset: reqQuery.page?parseInt(reqQuery.page):0
            }
        );
        // console.log(userData);
        ctx.body = util.resultSuccess({ data: userData.rows[0] });
    } catch (err) {
        ctx.body = util.resultError({ data: null });
    }
})
/**
 * 修改
 * */
router.post('/editById', async (ctx) => {
    try {
        ctx
            .validateBody("id")
            .required("必须填写id")
            .isString()
            .trim()
        ctx
            .validateBody("rtitle")
            .required("标题不能为空")
            .isString()
            .trim()
        ctx
            .validateBody("rcontent")
            .required("内容不能为空")
            .isString()
            .trim()
        let reqBody = ctx.request.body;

        let requestData = await userinfo.update({
            rtitle: reqBody.rtitle ? reqBody.rtitle : '',
            rcontent: reqBody.rcontent ? reqBody.rcontent : '',
            rcontent_html: reqBody.rcontent_html ? reqBody.rcontent_html : '',
        }, {
            where: {
                id: reqBody.id
            }
        })
        if (requestData[0] > 0) {
            ctx.body = util.resultSuccess({ message: '修改成功', data: null });
        } else {
            ctx.body = util.resultError({ code: 0, message: '修改失败,id错误', data: null });
        }

    } catch (err) {
        console.log(err);
        ctx.body = util.resultError({ code: 0, message: err.message, data: null });
    }
})

/**
 * 新增
 * */
router.post('/userAdd', async (ctx) => {
    try {
        ctx
            .validateBody("rtitle")
            .required("标题不能为空")
            .isString()
            .trim()
        ctx
            .validateBody("rcontent")
            .required("内容不能为空")
            .isString()
            .trim()

        let reqBody = ctx.request.body;
        let requestData = await userinfo.create({
            rtitle: reqBody.rtitle ? reqBody.rtitle : '',
            rcontent: reqBody.rcontent ? reqBody.rcontent : '',
            rcontent_html: reqBody.rcontenthtml ? reqBody.rcontenthtml : '',
            rt_id: reqBody.rtid ? reqBody.rtid : '',
        })
        if (requestData.dataValues) {
            ctx.body = util.resultSuccess({ message: '添加成功', data: requestData.dataValues });
        } else {
            ctx.body = util.resultSuccess({ code: 0, message: '添加失败', data: null });
        }
    } catch (err) {
        ctx.body = util.resultError({ code: 0, message: err.message, data: null });
    }
})

/**
 * 删除
 * */
router.post('/delById', async (ctx) => {
    try {
        ctx
            .validateBody("id")
            .required("id不能为空")
            .isString()
            .trim()

        let requestData = await userinfo.destroy({
            where: {
                id: ctx.vals.id
            }
        })
        if (requestData > 0) {
            ctx.body = util.resultSuccess({ message: '删除成功' });
        } else {
            ctx.body = util.resultSuccess({ code: 0, message: '删除失败,该类型不存在', data: null });
        }
    } catch (err) {
        ctx.body = util.resultError({ code: 0, message: err.message, data: null });
    }
})

module.exports = router.routes(); 